/**
 * PhotoBrowserServlet.java
 */
package com.example.myproject;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@SuppressWarnings("serial")
public class PhotoBrowserServlet extends HttpServlet {


	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		PictureUploadDAO photo = new PictureUploadDAO();
		List<PictureUploadDTO> list = photo.list();
		HashMap<String, String> result = new HashMap<String, String>();
		StringBuffer resultHtml = new StringBuffer();
			
		/* to retrieve music genre from   AudioUploadDAO */
		for(int i=0; i < list.size(); i++)
		{	
			if(!"".equals(list.get(i).getCaption()) && list.get(i).getCaption() != null)
			{
				if(i==0)
				{
					resultHtml.append("<li>");
					resultHtml.append("<a href=photobrowser?fileType=image&sortType="+list.get(i).getCaption()+">");
					resultHtml.append(list.get(i).getCaption());
					resultHtml.append("</a></li>");
					result.put(list.get(i).getCaption(), list.get(i).getCaption());
				}
				else
				{
					if(!result.containsKey(list.get(i).getCaption()))
					{
						resultHtml.append("<li>");
						resultHtml.append("<a href=photobrowser?fileType=image&sortType="+list.get(i).getCaption()+">");
						resultHtml.append(list.get(i).getCaption());
						resultHtml.append("</a></li>");
						result.put(list.get(i).getCaption(), list.get(i).getCaption());
					}
				}
			}
		}

		req.setAttribute("imagesort", resultHtml);
			
		RequestDispatcher view = req.getRequestDispatcher("index.jsp");
        view.forward(req, resp);	

	}

}